package com.example.service.impl;

import com.example.domain.Song;
import com.example.domain.SonglistSong;
import com.example.exception.Result;
import com.example.mapper.SongMapper;
import com.example.mapper.SonglistSongMapper;
import com.example.service.SongService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Date;
import java.util.List;
@Service

/**
 * @Description:
 * @Author: Ma_XiaoLong
 * @CreateTime： 2024/1/18 10:04
 */
public class SongServiceImpl implements SongService {
    @Autowired
    private SongMapper songMapper;
    private SonglistSongMapper songlistSongMapper;
/**
 * @Description: 删除歌单下的歌曲（接触某个歌单与某个歌曲的关联关系)
 * @Author: Ma_XiaoLong
 * @CreateTime: 2024/1/19 9:38
 * @param: songlistId
 * @param: songId
 * @return: int
 **/
    @Override
    public int deleteSongBySonglistIdSongId(Integer songlistId, Integer songId) {
        return songlistSongMapper.deleteBySonglistIdSongId(songlistId,songId);
    }

    /**
     * @Description: 根据歌单id查询所有歌曲列表（没有分页）
     * @Author: Ma_XiaoLong
     * @CreateTime: 2024/1/18 10:06
     * @param: songlistId
     * @return: java.util.List<com.example.domain.Song>
     **/
    @Override
    public List<Song> getListBySonglistId(Integer songlistId) {
        return songMapper.getListBySonglistId(songlistId);
    }

    //声明式事务注解
    @Override
    public int addSongToSonglist(Integer songlistId, Song song) {
        //step1:[写操作】将歌曲添加到歌曲表中;
        song.setCreateTime(new Date());
        int rows = songMapper.insertSong(song);
        //step2:【写操作】将歌曲添加到歌单中（建立关联关系）：添加中间表记录
        if (rows == 1) {

            SonglistSong songlistSong = new SonglistSong(songlistId, song.getId());
            rows = SonglistSongMapper.insert(songlistSong);
        }
        return rows;
    }

    @Override
    public List<Song> getListBySong(Song song) {
        //开启分页，将页码，每页记录数
        PageHelper.startPage(song.getPageNum(),song.getPageSize());
        List<Song> songs = songMapper.selectListBySong(song);
        return songs;
    }
    /**
     * @Description: 根据id查询歌曲
     * @Author: Ma_XiaoLong
     * @CreateTime: 2024/1/29 15:41
     * @param: id
     * @return: com.example.domain.Song
     **/
    //根据id查询歌曲
    @Override
    public Song getById(Integer id) {
        return songMapper.getById(id);
    }
    /**
     * @Description: 根据id修改歌曲信息
     * @Author: Ma_XiaoLong
     * @CreateTime: 2024/1/29 15:41
     * @param: song
     * @return: int
     **/
    //根据id修改歌曲信息

    public int updateById(Song song) {
        return songMapper.updateById(song);
    }
    /**
     * @Description: 新增歌曲
     * @Author: Ma_XiaoLong
     * @CreateTime: 2024/1/29 15:41
     * @param: song
     * @return: int
     **/
    //新增歌曲

    public int saveSong(Song song) {
        return songMapper.addSong(song);
    }
    /**
     * @Description: 根据id删除歌曲
     * @Author: Ma_XiaoLong
     * @CreateTime: 2024/1/29 15:41
     * @param: id
     * @return: int
     **/
    //根据id删除歌曲
    @Override
    public int deleteById(Integer id) {
        return songMapper.deleteById(id);
    }


}